//
//  HTDetailViewController.m
//  HeartTravel
//
//  Created by lanou on 15/11/2.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "HTDetailViewController.h"
#import "HTImageViewController.h"
#import "HTMapViewController.h"
#import "UIImageView+WebCache.h"
@interface HTDetailViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *posterView;
@property (nonatomic, assign) CGFloat descHeight; // 先求出概况文本自定义高度
@property (nonatomic, assign) CGFloat arriveHeight; // 到达方式文本自定义高度
@property (nonatomic, assign) CGFloat sHeight;
@end

@implementation HTDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT + 5);
    _scrollView.bounces = YES;
    NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey:NSFontAttributeName];
    _descHeight = [self.detailModel.descrip boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size.height;
    
    _arriveHeight = [self.detailModel.arrival_type boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size.height;
    _scrollView.contentSize = CGSizeMake(0, 250 + _descHeight + 460 + _arriveHeight);
    _sHeight = 250 + _descHeight + 460 + _arriveHeight;
    _scrollView.delegate =self;
    [self.view addSubview:_scrollView];
    [self addDetailText];
    
    self.topView.backgroundColor = [UIColor colorWithWhite:0.367 alpha:1.000];
    self.topView.alpha = 0.5;
    UIButton *leftButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    leftButton.frame = CGRectMake(10, 15, 20, 20);
    [leftButton setBackgroundImage:[UIImage imageNamed:@"返回"] forState:(UIControlStateNormal)];
    [leftButton addTarget:self action:@selector(didClickToBack) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.topView];
    [self.topView addSubview:leftButton];
}

- (void)addDetailText{
    _posterView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250)];
    _posterView.image = [UIImage imageNamed:@"海"];
    [_posterView sd_setImageWithURL:[NSURL URLWithString:self.detailModel.cover]];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 260, SCREEN_WIDTH - 20, 30)];
    titleLabel.font = [UIFont systemFontOfSize:18];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.text = self.detailModel.name;
    [self.scrollView addSubview:titleLabel];
    
    UILabel *recommendedLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 290, SCREEN_WIDTH - 20, 50)];
    recommendedLabel.font = [UIFont systemFontOfSize:14];
    recommendedLabel.numberOfLines = 0;
    recommendedLabel.text = self.detailModel.recommended_reason;
    [self.scrollView addSubview:recommendedLabel];

    
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 345, SCREEN_WIDTH - 10, 1)];
    imageView1.backgroundColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:imageView1];
    
    UILabel *descLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 360, 80, 30)];
    descLabel.font = [UIFont systemFontOfSize:18];
    descLabel.text = @"概况";
    descLabel.textColor = [UIColor blackColor];
    descLabel.numberOfLines = 0;

    [self.scrollView addSubview:descLabel];

    UILabel *descDetailLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 390, SCREEN_WIDTH - 20, _descHeight)];
    descDetailLabel.font = [UIFont systemFontOfSize:13];
    descDetailLabel.textColor = [UIColor blackColor];
    descDetailLabel.numberOfLines = 0;
    descDetailLabel.text = self.detailModel.descrip;
    [self.scrollView addSubview:descDetailLabel];

    
    UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(10, _descHeight + 390 + 20, SCREEN_WIDTH - 10, 1)];
    imageView2.backgroundColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:imageView2];

    
    UILabel *addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, _descHeight + 425, 80, 30)];
    addressLabel.text = @"地址";
    addressLabel.font = [UIFont systemFontOfSize:18];
    addressLabel.textColor = [UIColor blackColor];
    [self.scrollView addSubview:addressLabel];
    
    CGFloat height = _descHeight + 440;
    UILabel *addressDetailLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, height + 10, SCREEN_WIDTH - 20, 60)];
    addressDetailLabel.font = [UIFont systemFontOfSize:13];
    addressDetailLabel.textColor = [UIColor blackColor];
    addressDetailLabel.numberOfLines = 0;
    addressDetailLabel.text = self.detailModel.address;
    [self.scrollView addSubview:addressDetailLabel];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(SCREEN_WIDTH - 35, height + 10 , 20, 20);
    [button setBackgroundImage:[UIImage imageNamed:@"下一步"] forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(didClickToMap) forControlEvents:(UIControlEventTouchUpInside)];
    [self.scrollView addSubview:button];
    
    UIImageView *imageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(10, height + 80, SCREEN_WIDTH - 10, 1)];
    imageView3.backgroundColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:imageView3];
    
    UILabel *arriveLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, height + 120, 120, 30)];
    arriveLabel.text = @"到达方式";
    arriveLabel.font = [UIFont systemFontOfSize:18];
    arriveLabel.textColor = [UIColor blackColor];
    [self.scrollView addSubview:arriveLabel];
    
    UILabel *arriveDetailLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, height + 170, SCREEN_WIDTH - 20, _arriveHeight)];
    arriveDetailLabel.font = [UIFont systemFontOfSize:13];
    arriveDetailLabel.textColor = [UIColor blackColor];
    arriveDetailLabel.numberOfLines = 0;
    arriveDetailLabel.text = self.detailModel.arrival_type;
    [self.scrollView addSubview:arriveDetailLabel];

    
    [self.scrollView addSubview:_posterView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat scrollViewY = scrollView.contentOffset.y;
    if (scrollView.isDragging) {
        if (scrollViewY < -130) {
            HTImageViewController *imageVC = [[HTImageViewController alloc]init];
            imageVC.model = self.detailModel;
            [self.navigationController pushViewController:imageVC animated:nil];
        }
        _posterView.frame = CGRectMake(scrollViewY/130 * 100, scrollViewY / 130 * 100, scrollViewY/ -130*(100 + 100) + SCREEN_WIDTH, -scrollViewY / 130 * 100 + 250);
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    _posterView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 250);
}
// 点击调用地图定位
- (void)didClickToMap{
    HTMapViewController *mapVC = [[HTMapViewController alloc]init];
    mapVC.lat = [self.detailModel.lat floatValue];
    mapVC.lng = [self.detailModel.lng floatValue];
    mapVC.titleName = self.detailModel.name;
    [self.navigationController pushViewController:mapVC animated:nil];
}

- (void)didClickToBack{
    [self.navigationController popViewControllerAnimated:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
